<template>
	<div>
    <!-- 新增收款 -->
      <FormPanel
        ref="formPanelA"
        :list="formList"
        :cols="3"
        labelWidth='100'
        :isSubmitBtn="false"
        formType="add"
        @formChange="formChangeHandle"
      />
		<div class="upWard" :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
      <el-button size="small" type="primary" @click="saveFunc">保存</el-button>
			<el-button size="small" @click="closeDialog">返回</el-button>
		</div>
	</div>
</template>

<script>
import moment from 'moment'
import { dictionary } from '@/utils/dictMixin';
import { notifyAction } from '@/utils';
import { addItem } from '@/api/capitalsettle/vehicle/settlecollection';
import { invoice8,numberCheck} from '@/utils/validate';
export default {
  name: 'AddReceiption',
  mixins: [dictionary],
  props: {
    rowData : {
      type: Object,
      default: () => ({})
    }
  },
	data (){
    this.saveData = null;
		return {
      formList: this.createFormList(),
		}
  },
  mounted (){
    this.init();
  },
	methods: {
    init (){
      let defaultData = '';
      let time = new Date();    //根据时间戳生成的时间对象
      let year = time.getFullYear();
      let month = time.getMonth()+1;
      let date = time.getDate() > 9 ? time.getDate() : '0' + time.getDate();
      let hours = time.getHours() > 9 ? time.getHours() : '0' + time.getHours();
      let minutes = time.getMinutes() > 9 ? time.getMinutes() : '0' + time.getMinutes();
      defaultData = `${year}-${month}-${date}`;
      console.log(this.rowData,"this.rowData",this.rowData.receivableAmount,this.formList.find(item => item.fieldName == 'receivableAmount').initialValue)
      this.formList.find(item => item.fieldName == 'receiveDate').initialValue = defaultData
      this.formList.find(item => item.fieldName == 'balanceNo').initialValue = this.rowData.balanceNo
      this.formList.find(item => item.fieldName == 'receivableAmount').initialValue = this.rowData.receivableAmount
    },
		createFormList () {
			return [
				{
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'balanceNo',
          initialValue: '',
          rules: [{ required: true, message: '请核实', trigger: 'blur'}],
          disabled: true,
        },
        {
          type: 'SELECT',
          label: '收款方式',
          fieldName: 'payTypeCode',
          initialValue: '',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'blur'}],
          itemList: this.createDictList('8044')
        },
        {
          type: 'INPUT',
          label: '收款金额',
          fieldName: 'receiveAmount',
          initialValue: '',
          placeholder: '请填写...',
          rules: [{ required: true, message: '请填写...', trigger: 'blur'},{ required: true, validator: numberCheck, trigger: 'blur'}],
        },
        {
          type: 'INPUT',
          label: '发票号码',
          fieldName: 'billNo',
          initialValue: '',
          maxlength:8,
          placeholder: '请填写...',
          rules: [{ required: true, validator: invoice8, trigger: 'blur'}],
        },
        {
          type: 'DATE',
          label: '收款日期',
          fieldName: 'receiveDate',
          dateType:"exactdate",
          initialValue: '',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'blur'}],
        },
        {
          type: 'INPUT',
          label: '付款人',
          fieldName: 'payman',
          initialValue: ''
        },
        {
          // type: 'INPUT_NUMBER',
          type: 'INPUT',
          label: '应收金额',
          fieldName: 'receivableAmount',
          initialValue: '',
          placeholder: '请填写...',
          disabled: true
          // rules: [{ required: true, message: '请填写...', trigger: 'blur'}],
        },
        {
          type: 'INPUT',
          label: '经办人',
          fieldName: 'handler',
          initialValue: '',
          placeholder: '请填写...',
        },
        {
          type: 'SELECT',
          label: '是否销账',
          fieldName: 'writeoffTag',
          initialValue: '',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '销账人',
          fieldName: 'writeoff',
          initialValue: '',
          placeholder: '请填写...',
        },
        {
          type: 'DATE',
          label: '销账日期',
          fieldName: 'writeoffDate',
          initialValue: '',
          dateType:"exactdate",
          placeholder: '',
          offsetRightCols: 1
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          initialValue: '',
          placeholder: '',
        },
			]
    },
    saveFunc (){
      this.$refs.formPanelA.SUBMIT_FORM();
      console.log('saveFunc --- before', this.saveData)
      if (this.saveData !== null) {
        this.saveData['pubPayId'] = this.rowData['pubPayId']
        let receiveDateM='' ;
        [1,2,3,4,5,6,7,8,9].forEach(x=>{
          if(this.saveData.receiveDate.split('-')[1] == x && this.saveData.receiveDate.split('-')[1].length == 1){
              receiveDateM = '0'+this.saveData.receiveDate.split('-')[1]
              this.saveData.receiveDate = `${this.saveData.receiveDate.split('-')[0]}-${receiveDateM}-${this.saveData.receiveDate.split('-')[2]}`
          }
        })
        addItem(this.saveData).then(res => {
          if (res.success) {
            notifyAction('保存成功');
            this.closeDialog('refresh');
          } else {
            notifyAction(res.errMsg, 'error');
          }
        })
      }
    },
    closeDialog (val = ''){ //返回上层
      this.$emit('close', val);
    },
    formChangeHandle(val) {
      this.saveData = _.cloneDeep(val);
    },
	}

}
</script>

<style scoped="scoped" lang="less">
</style>
